const util = require('../../utils/util.js')
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        withdrawBack: false,
        scrollHeight: '',
        currtab: 0,
        noMore: false,
        refreshData: [{
                page: 1,
                size: 10,
                isEnd: false
            }, {
                page: 1,
                size: 10,
                isEnd: false
            }, {
                page: 1,
                size: 10,
                isEnd: false
            }], //依次代表 账户余额 充值明细 提现记录 分页信息
        swipertab: [{
                name: '账户余额',
                index: 0
            }, {
                name: '充值明细',
                index: 1
            }, {
                name: '提现记录',
                index: 2
            }],
        deposit: '0.00',
        data_balance: [], //余额数据
        data_recharge: [], //充值数据
        data_withdraw: [] //提现数据
    },
    computeScrollViewHeight() {
        let that = this
        let query = wx.createSelectorQuery().in(this)
        query.select('.deposit-header').boundingClientRect()
        query.select('.deposit-tab-p').boundingClientRect()
        query.exec(res => {
            let headerHeight = res[0].height
            let tabHeight = res[1].height
            let windowHeight = wx.getSystemInfoSync().windowHeight
            let scrollHeight = windowHeight - headerHeight - tabHeight
            this.setData({
                scrollHeight: scrollHeight
            })
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        util.share();
        var self = this;
        wx.getStorage({
            key: 'key',
            success: (res) => {
                self.setData({
                    member_key: res.data
                });
                self.getBalance();
                self.getData();
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let self = this;
        if (self.data.withdrawBack) {
            self.setData({
                currtab: 2,
            });
            self.data.refreshData[2].page = 1;
            this.getData();
        }
    },
    /**
     * 获取余额
     */
    getBalance: function () {
        var self = this;
        //获取余额
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_index&op=my_asset&key=' + self.data.member_key + '&fields=predepoit',
            success(res) {
                self.setData({
                    deposit: res.data.datas.predepoit
                });
            }
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.computeScrollViewHeight();
    },

    /**
     * tab点击事件
     */
    tabClick: function (event) {
        var self = this;
        var tab = event.currentTarget.dataset.tab;
        self.setData({
            currtab: tab,
        });
        var self = this;
        switch (self.data.currtab) {
            //账户余额
            case 0:
                if (self.data.data_balance.length == 0) {
                    self.data.refreshData[0].page = 1;
                    this.getData();
                }
                break;
                //充值明细
            case 1:
                if (self.data.data_recharge.length == 0) {
                    self.data.refreshData[1].page = 1;
                    this.getData();
                }
                break;
                //提现记录
            case 2:
                if (self.data.data_withdraw.length == 0) {
                    self.data.refreshData[2].page = 1;
                    this.getData();
                }
                break;
        }
    },
    /**
     * 获取数据
     */
    getData: function () {
        var self = this;
        if (self.data.refreshData[self.data.currtab].isEnd) {
            return;
        }
        wx.showLoading({
            title: "加载中",
        })
        var d_url = '/index.php?act=member_fund';
        switch (self.data.currtab) {
            //账户余额
            case 0:
                d_url += '&op=predepositlog&key=' + self.data.member_key + '&curpage=' + self.data.refreshData[0].page + '&page=' + self.data.refreshData[0].size;
                break;
                //充值明细
            case 1:
                d_url += '&op=pdrechargelist&key=' + self.data.member_key + '&curpage=' + self.data.refreshData[1].page + '&page=' + self.data.refreshData[1].size;
                break;
                //提现记录
            case 2:
                d_url += '&op=pdcashlist&key=' + self.data.member_key + '&curpage=' + self.data.refreshData[2].page + '&page=' + self.data.refreshData[2].size;
                break;
        }
        //获取列表数据
        wx.request({
            url: app.globalData.ApiUrl + d_url,
            success(res) {
                let tempData = [];
                switch (self.data.currtab) {
                    //账户余额
                    case 0:
                        //如果是第一页数据只添加新的数据 
                        self.data.refreshData[self.data.currtab].page > 1 ? tempData = self.data.data_balance.concat(res.data.datas.list) : tempData = res.data.datas.list;
                        self.setData({
                            data_balance: tempData, //获取数据数组   
                        });
                        break;
                        //充值明细
                    case 1:
                        //如果是第一页数据只添加新的数据 
                        self.data.refreshData[self.data.currtab].page > 1 ? tempData = self.data.data_recharge.concat(res.data.datas.list) : tempData = res.data.datas.list;
                        self.setData({
                            data_recharge: tempData, //获取数据数组   
                        });
                        break;
                        //提现记录
                    case 2:
                        //如果是第一页数据只添加新的数据 
                        self.data.refreshData[self.data.currtab].page > 1 ? tempData = self.data.data_withdraw.concat(res.data.datas.list) : tempData = res.data.datas.list;
                        self.setData({
                            data_withdraw: tempData, //获取数据数组   
                        });
                        break;
                }
                if (!res.data.hasmore) {
                    self.data.refreshData[self.data.currtab].isEnd = true; //把“没有数据”设为true，显示  
                    self.setData({
                        refreshData: self.data.refreshData
                    });
                }
            },
            fail: function () {
                wx.showToast({
                    title: '数据加载失败',
                    icon: none
                })
            },
            complete: function () {
                wx.hideLoading();
            }
        });
    },
    //滚动到底部触发事件  
    searchScrollLower: function () {
        let that = this;
        if (!that.data.refreshData[that.data.currtab].isEnd) {
            that.data.refreshData[that.data.currtab].page = that.data.refreshData[that.data.currtab] + 1;
            that.getData();
        }
    },
    withdrawCommit: function () {
        let self = this;
        if (self.data.deposit > 0) {
            wx.navigateTo({
                url: '../property_withdraw_page1/withdraw_page1'
            })
        } else {
            wx.showToast({
                title: '余额为零',
                icon: 'none'
            })
        }
    }
})